<?php

namespace C\S\Contract;

use C\L\Service;
use C\M\Contract as ContractModel;

class Contract extends Service
{
    protected function setModel()
    {
        $this->model = new ContractModel();
    }

  /**
   * 列出合同
   *
   * @param int $currentPage
   * @param int $perPage
   * @return mixed
   **/
    public function paginate($currentPage, $perPage)
    {
        $builder = ContractModel::query()
        ->orderBy("id ASC")
        ->createBuilder();

        return $this->paginateByBuilder($builder, $currentPage, $perPage);
    }

  /**
   * 查看合同
   *
   * @param int $id
   * @return mixed
   **/
    public function view($id)
    {
        return ContractModel::findById($id);
    }

  /**
   * 添加合同
   *
   * @param array $params
   * @return mixed
   **/
    public function store($params)
    {
        $contract = new ContractModel();

        return $contract->save($params)
        ? $contract
        : false;
    }

  /**
   * 更新合同
   *
   * @param int $id
   * @param array $params
   * @return mixed
   **/
    public function newUpdate($id, $params)
    {
        $contract = ContractModel::findById($id);

        if (!$contract) {
            return false;
        }

        return $contract->save($params)
        ? $contract
        : false;
    }

  /**
   * 删除合同
   *
   * @param int $id
   * @param array $params
   * @return mixed
   **/
    public function delete($id)
    {
        $contract = ContractModel::findById($id);

        if (!$contract) {
            return false;
        }

        return $contract->delete();
    }
}
